package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.BeanUtil;
import com.macro.mall.common.util.StringIf;
import com.macro.mall.dao.OmsOrderDao;
import com.macro.mall.domain.MemberReadHistory;
import com.macro.mall.dto.OmsOrderDetail;
import com.macro.mall.mapper.OmsOrderMapper;
import com.macro.mall.mapper.UmsIntegrationChangeHistoryMapper;
import com.macro.mall.mapper.UmsMemberMapper;
import com.macro.mall.model.*;
import com.macro.mall.repository.MemberReadHistoryRepository;
import com.macro.mall.service.UmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UmsMemberServiceImpl implements UmsMemberService {
    @Autowired
    private UmsMemberMapper memberMapper;
    @Autowired
    private OmsOrderMapper orderMapper;
    @Autowired
    private OmsOrderDao orderDao;
    @Autowired
    private UmsIntegrationChangeHistoryMapper integrationChangeHistoryMapper;
    @Autowired
    private MemberReadHistoryRepository memberReadHistoryRepository;

    @Override
    public CommonResult<List<UmsMember>> getMemberList(UmsMember umsMember, Integer pageSize, Integer pageNum) {
        UmsMemberExample example = new UmsMemberExample();
        UmsMemberExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(umsMember.getUsername())) {
            //用户名
            criteria.andUsernameLike("%" + umsMember.getUsername() + "%");
        }
        if (umsMember.getType() != null) {
            //用户还是分销员
            criteria.andTypeEqualTo(umsMember.getType());
        }
        if (umsMember.getMemberLevelId() != null && umsMember.getMemberLevelId() != 1) {
            //会员id!=1并且会员结束时间大于当前时间
            criteria.andMemberLevelIdEqualTo(umsMember.getMemberLevelId()).andMemberLevelEndTimeGreaterThan(new Date());
        }
        if (umsMember.getMemberLevelId() != null && umsMember.getMemberLevelId() == 1) {
            //非会员
            criteria.andMemberLevelIdEqualTo(umsMember.getMemberLevelId());
        }
        List<UmsMember> result = new ArrayList<>();
        List<UmsMember> umsMembers = memberMapper.selectByExample(example);
        if (umsMembers != null && umsMembers.size() > 0) {
            return CommonResult.success(umsMembers);
        }
        return CommonResult.success(result);
    }

    @Override
    public CommonResult bindingStore(long memberId, long storeId) {
        UmsMember umsMember = new UmsMember();
        umsMember.setId(memberId);
        umsMember.setStoreId(storeId);
        int count = memberMapper.updateByPrimaryKey(umsMember);
        if(count > 0){
            return CommonResult.success(count);
        }else{
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult createDistributionMember(UmsMember umsMember) {
        umsMember.setType(1);//1为分销员
        umsMember.setUuid(StringIf.getUUId());
        int count = memberMapper.insert(umsMember);
        if(count > 0){
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult updateDistributionMember(long id, UmsMember umsMember) {
        umsMember.setId(id);
        int count = memberMapper.updateByPrimaryKey(umsMember);
        if(count > 0){
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult deleteDistributionMember(long id) {
        int count = memberMapper.deleteByPrimaryKey(id);
        if(count > 0){
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult deleteDistributionMemberList(List<Long> ids) {
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andIdIn(ids);
        int count = memberMapper.deleteByExample(example);
        if(count > 0){
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult<UmsMember> getMemberById(Long id) {
        UmsMember member = memberMapper.selectByPrimaryKey(id);
        return CommonResult.success(member);
    }

    @Override
    public CommonResult<UmsMember> getMemberByPhone(String phone) {
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andPhoneEqualTo(phone);
        List<UmsMember> memberList = memberMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(memberList)) {
            return CommonResult.success(memberList.get(0));
        }else {
            return CommonResult.success(new UmsMember());
        }
    }

    @Override
    public CommonResult<CommonPage<OmsOrderDetail>> getOrderByMember(Long memberId, Integer pageNum, Integer pageSize) {
        OmsOrderExample example = new OmsOrderExample();
        example.createCriteria().andDeleteStatusEqualTo(0)
                                .andMemberIdEqualTo(memberId);
        PageHelper.startPage(pageNum,pageSize);
        //用户的所有订单
        List<OmsOrder> omsOrders = orderMapper.selectByExample(example);
        CommonPage<OmsOrder> commonPage = CommonPage.restPage(omsOrders);
        List<OmsOrder> list = commonPage.getList();
        List<OmsOrderDetail> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (OmsOrder order : list) {
                OmsOrderDetail detail = new OmsOrderDetail();
                detail = orderDao.getDetail(order.getId());
                result.add(detail);
            }
        }
        CommonPage<OmsOrderDetail> pageResult = new CommonPage<>();
        BeanUtils.copyProperties(commonPage,pageResult);
        pageResult.setList(result);
        return CommonResult.success(pageResult);
    }

    @Override
    public CommonResult<CommonPage<UmsIntegrationChangeHistory>> getIntegrationHistoryByMember(Long memberId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        UmsIntegrationChangeHistoryExample example = new UmsIntegrationChangeHistoryExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        example.setOrderByClause("id desc");
        List<UmsIntegrationChangeHistory> umsIntegrationChangeHistories = integrationChangeHistoryMapper.selectByExample(example);
        List<UmsIntegrationChangeHistory> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(umsIntegrationChangeHistories)) {
            return CommonResult.success(CommonPage.restPage(umsIntegrationChangeHistories));
        } else {
            return CommonResult.success(CommonPage.restPage(result));
        }
    }

    @Override
    public Page<MemberReadHistory> getReadProductHistoryByMember(Long memberId, Integer pageNum, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<MemberReadHistory> byMemberIdOrderByCreateTimeDesc = memberReadHistoryRepository.findByMemberIdOrderByCreateTimeDesc(memberId, pageable);
        //处理分页
        return byMemberIdOrderByCreateTimeDesc;
    }

    @Override
    public CommonResult<CommonPage<OmsOrderDetail>> getCommissionHistory(Long agentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        OmsOrderExample orderExample = new OmsOrderExample();
        OmsOrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andDeleteStatusEqualTo(0)
                .andAgentIdEqualTo(agentId)
                .andStatusEqualTo(3);
        orderExample.setOrderByClause("create_time desc");
        List<OmsOrder> orderList = this.orderMapper.selectByExample(orderExample);
        CommonPage<OmsOrder> commonPage = CommonPage.restPage(orderList);
        List<OmsOrder> list = commonPage.getList();
        List<OmsOrderDetail> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (OmsOrder order : list) {
                OmsOrderDetail detail = new OmsOrderDetail();
                detail = orderDao.getDetail(order.getId());
                result.add(detail);
            }
        }
        CommonPage<OmsOrderDetail> pageResult = new CommonPage<>();
        BeanUtils.copyProperties(commonPage,pageResult);
        pageResult.setList(result);
        return CommonResult.success(pageResult);
    }

}
